//
//  LZRestaurantTableViewCell.m
//  laziz_Consumer
//
//  Created by XMac on 16/10/24.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZRestaurantTableViewCell.h"
#import "Masonry.h"
#import "HCSStarRatingView.h"
#import "BlocksKit+UIKit.h"
#import "LZLocationDistanceView.h"
#import "LZRestaurantCellButton.h"
#import "LZRestaurantListModel.h"
#import "UIImageView+WebCache.h"
#import "UIButton+EnlargeEdge.h"

@interface LZRestaurantTableViewCell ()

@property (strong, nonatomic) IBOutlet UIImageView *restaurantImageView;

@property (strong, nonatomic) IBOutlet UILabel *titleLabel;

@property (strong, nonatomic) IBOutlet UILabel *detailLabel;

@property (strong, nonatomic) IBOutlet UIImageView *seize_ImageView;

@property (strong, nonatomic) IBOutlet UIImageView *catch_group_ImageView;

@property (strong, nonatomic) IBOutlet UILabel *per_capita_Label;
@property (weak, nonatomic) IBOutlet UIView *ssdd;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *titleLabelTrailingConstraint;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *catchGroupImageViewLeadingToSeizeImageViewConstraint;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *catchGroupImageViewLeadingToBgViewConstraint;

@property (strong, nonatomic) LZLocationDistanceView *distanceView;

@property (strong, nonatomic) HCSStarRatingView *starRatingView;

@property (strong, nonatomic)  UILabel *labNoReviewRemind;

@property (strong, nonatomic) LZRestaurantCellButton *button;

@end


@implementation LZRestaurantTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.contentView.translatesAutoresizingMaskIntoConstraints = YES;
    
    self.per_capita_Label.layer.cornerRadius = 2.0;
    self.per_capita_Label.layer.borderWidth = 0.5;
    self.per_capita_Label.layer.borderColor = UIColorFromRGB(0xcccccc, 1).CGColor;
    
    /*
     ==========================================Star =================================================
     */
    [self createStarView];
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    [self configUI];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

#pragma
#pragma mark - Public method

- (void)setModel:(LZRestaurantListModel *)model {

    _model = model;
    
    [self.restaurantImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",APIBaseImageUrl,model.sellerPhoto]] placeholderImage:[UIImage imageNamed:@"lz_other_placehoiderImage"]];
    
    self.titleLabel.text = model.restaurantName;
    self.detailLabel.text = [model.cookingStyle stringByReplacingOccurrencesOfString:@"," withString:@" "];
    
    // 评分
    BOOL isHaveReview = model.averageScore>0;
    self.starRatingView.hidden = !isHaveReview;
    self.labNoReviewRemind.hidden = isHaveReview;
    if (isHaveReview) {
        self.starRatingView.value = model.averageScore;
    }
   
    self.per_capita_Label.text = [NSString stringWithFormat:@" %.3f%@ ", model.averageConsumption,LZLocalizedString(@"restaurant_money_person", nil)];
    self.seize_ImageView.hidden = !model.isCoupons;
    self.catch_group_ImageView.hidden = !model.isGroupons;
    
    if (model.isCoupons == 0) {
        self.seize_ImageView.hidden = YES;
        self.catchGroupImageViewLeadingToSeizeImageViewConstraint.priority = 250;
        self.catchGroupImageViewLeadingToBgViewConstraint.priority = 999;
    }
    else {
        self.catchGroupImageViewLeadingToSeizeImageViewConstraint.priority = 999;
        self.catchGroupImageViewLeadingToBgViewConstraint.priority = 250;
    }
   
    
    if (model.isGroupons == 0) {
        self.catch_group_ImageView.hidden = YES;
    }
    
    if (self.button) {
        if (self.type == CellType_MyFavorites) {
            if (model.isStore) {
                // LZLocalizedString(@"mine_my_favorites", @"")
                [self.button setContent:@"" andColor:[UIColor clearColor] andImage:[UIImage imageNamed:@"首页_关注_已收藏"]];
            }
            else {
                // LZLocalizedString(@"mine_my_favorites", @"")
                [self.button setContent:@"" andColor:[UIColor clearColor] andImage:[UIImage imageNamed:@"关注"]];
            }
        }
    }
    
    [self setTitleLabelTrailingConstraintConstant];
}

#pragma
#pragma mark - Private method
-(void)configUI{
    switch (self.type) {
        case CellType_Normal:
        {
            /*
             ==========================================距离=================================================
             */
            
        }
            break;
        case CellType_MyToBeReviewed:
        {
            /*
             ==========================================待评价=================================================
             */
            [self createButtonControlView];
        }
            break;
        case CellType_MyFavorites:
        {
            /*
             ==========================================收藏=================================================
             */
            [self createButtonControlView];
        }
            break;
    }
    
    [self setTitleLabelTrailingConstraintConstant];
}

// 根据类型设置 titleLabelTrailingConstraint的constant值
- (void)setTitleLabelTrailingConstraintConstant {
    switch (self.type) {
        case CellType_Normal:
        {
            /*
             ==========================================距离=================================================
             */
            if (self.isShowDistance) {
                [self.distanceView setDistanceValue:[NSString stringWithFormat:@"%dm",(int)self.model.distance]];
                self.titleLabelTrailingConstraint.constant = self.distanceView.intrinsicContentSize.width+10;
            }
            else {
                self.titleLabelTrailingConstraint.constant = 10;
            }
        }
            break;
        case CellType_MyToBeReviewed:
        {
            /*
             ==========================================待评价=================================================
             */
            // 1、距离图片：10  2、图片宽度：8  3、图片到人均消费的label的宽度：6  4、人均消费的label到右边的距离：10
            self.titleLabelTrailingConstraint.constant = 10+self.button.intrinsicContentSize.width+10;
        }
            break;
        case CellType_MyFavorites:
        {
            /*
             ==========================================收藏=================================================
             */
            // 1、距离图片：10  2、图片宽度：8  3、图片到人均消费的label的宽度：6  4、人均消费的label到右边的距离：10
            self.titleLabelTrailingConstraint.constant = 10+self.button.intrinsicContentSize.width+10;
        }
            break;
    }
}

// 创建距离的View

- (LZLocationDistanceView*)distanceView {
    if (_distanceView) {
        return _distanceView;
    }
    
    _distanceView = [[LZLocationDistanceView alloc]initWithText:@"500m"];
    [self addSubview:_distanceView];
    
    [_distanceView makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(-10);
        make.centerY.equalTo(self.titleLabel.mas_centerY);
    }];
    
    return _distanceView;
}


// 创建星星的view
- (void)createStarView {
    if (self.starRatingView) {
        return;
    }
    CGFloat singleWidth = 16;
    int number = 5;
    self.starRatingView = [[HCSStarRatingView alloc]initWithFrame:CGRectZero];
    self.starRatingView.maximumValue = number;
    self.starRatingView.minimumValue = 0;
    self.starRatingView.value = number;
    self.starRatingView.spacing = 2.5;
    self.starRatingView.allowsHalfStars = YES;
    self.starRatingView.accurateHalfStars = YES;

    self.starRatingView.tintColor = UIColorFromRGB(0xF08F4D, 1);
    self.starRatingView.userInteractionEnabled = NO;
    [self.starRatingView bk_addEventHandler:^(id sender) {
        //
    } forControlEvents:UIControlEventValueChanged];
    [self.contentView addSubview:self.starRatingView];
    
    [self.starRatingView makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(_seize_ImageView.leading);
        make.bottom.equalTo(self.seize_ImageView.mas_top).offset(-10);
        make.size.equalTo(CGSizeMake(singleWidth * number, 10));
    }];
    
    [self createNoReviewRemindLabel];
}

// 没有评分 的提示
- (void)createNoReviewRemindLabel {
    _labNoReviewRemind = [[UILabel alloc] init];
    self.labNoReviewRemind.text = LZLocalizedString(@"restaurant_norating_now", nil);
    self.labNoReviewRemind.font = [UIFont systemFontOfSize:14];
    self.labNoReviewRemind.textColor = UIColorFromRGB(0xcccccc, 1);
    [self.contentView addSubview:self.labNoReviewRemind];
    
    [self.labNoReviewRemind mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.starRatingView);
        make.centerY.equalTo(self.starRatingView);
    }];
}

// 创建点击按钮的view
- (void)createButtonControlView {

    if (self.button) {
        return;
    }
    
    self.button = [[LZRestaurantCellButton alloc] init];
    if (self.type == CellType_MyToBeReviewed) {
        // LZLocalizedString(@"review_write", nil)
        [self.button setContent:@"" andColor:[UIColor clearColor] andImage:[UIImage imageNamed:@"餐厅_待评价"]];
    }
    else if (self.type == CellType_MyFavorites) {
        // LZLocalizedString(@"mine_my_favorites", @"")
        [self.button setContent:@"" andColor:[UIColor clearColor] andImage:[UIImage imageNamed:@"首页_关注_已收藏"]];
    }
    [self.button setEnlargeEdge:30];
    [self.button addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.contentView addSubview:self.button];
    
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.contentView).offset(-10);
        make.centerY.equalTo(self.titleLabel.mas_centerY);
    }];
}

// 按钮点击事件
- (void)buttonClickAction:(LZRestaurantCellButton*)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickButtonWithModel:andIndexPath:andResultBlock:)]) {
        WS(weakSelf)
        [self.delegate clickButtonWithModel:self.model andIndexPath:self.indexPath andResultBlock:^(BOOL isFreshButton) {
            if (isFreshButton) {
                if (weakSelf.type == CellType_MyFavorites) {
                    weakSelf.model.isStore = !weakSelf.model.isStore;
                    if (weakSelf.model.isStore) {
                        // LZLocalizedString(@"mine_my_favorites", @"")
                        [weakSelf.button setContent:@"" andColor:[UIColor clearColor] andImage:[UIImage imageNamed:@"首页_关注_已收藏"]];
                    }
                    else {
                        // LZLocalizedString(@"mine_my_favorites", @"")
                        [weakSelf.button setContent:@"" andColor:[UIColor clearColor] andImage:[UIImage imageNamed:@"关注"]];
                    }
                }
            }
        }];
    }
}

@end
